<?php
class Api{
    static $table = "role1";
    function get($req){
        $table = self::$table;
        $id = $req->param;
        $row = pt\db\Mysql::init()->row("select *,id as `key` from {$table} where id = ?",$id);
        return $row;
    }
    function post($req){
        $name = $req->body->name;

        if(!$name || strlen($name) < 2){
            throw new ErrHttp("名称至少2位",500);
        }
        $row = array(
            "name"=>$name,
            //"add_time"=>date("Y-m-d H:is")
        );
        $id = pt\db\Mysql::init()->insert(self::$table,$row);
        return array(
            "id"=>$id
        );
    }
    function put($req){
        $id = $req->param;

        $name = empty($req->body->name)?null:$req->body->name;

        $row = array();
        if($name){
            $row['name'] = $name;
        }

        pt\db\Mysql::init()->update(self::$table,$row,array("id"=>$id));

        return array(
            "message"=>"修改成功"
        );
    }
    function delete($req){
        $id = $req->param;
        pt\db\Mysql::init()->delete(self::$table,array("id"=>$id));
        return array(
            "message"=>"删除成功"
        );
    }
}
